package l.l.w.lianjia.TWO;

import org.junit.Test;

import java.util.*;

/**
 * Created by llw on 2018/3/19/0019.
 */
public class Main {

    @Test
    public void max(){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[][] routs = new int[n][n];
        for(int i = 0; i < n-1; ++i){
            int u = scanner.nextInt();
            int v = scanner.nextInt();
            int c = scanner.nextInt();
            routs[u-1][v-1] = c;
            routs[v-1][u-1] = c;
        }
        int q = scanner.nextInt();
        for(int i = 0; i < q; ++i) {
            int u = scanner.nextInt();
            int v = scanner.nextInt();
            getMax(routs,u,v);
        }
    }

    private int getMax(int[][] routs, int u, int v) {
        if(routs[u-1][v-1] != 0) return routs[u-1][v-1];
        for(int i = 0; i < routs[0].length;++i){

        }
        // TODO
        return  0;
    }


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        HashMap<Integer,Integer>[] maps = new HashMap[n];
        for(int i = 0; i < n; ++i){
            maps[i] = new HashMap<>(2);
        }
        for(int i = 0; i < n-1; ++i){
            int u = scanner.nextInt();
            int v = scanner.nextInt();
            int c = scanner.nextInt();
            maps[u-1].put(v,c);
            maps[v-1].put(u,c);
        }
        int q = scanner.nextInt();
        for(int i = 0; i < q; ++i){
            int u = scanner.nextInt();
            int v = scanner.nextInt();
            int[] temp = new int[n];
            temp[u-1] = 1;
            int max = getLength(maps, u, v,0,temp);
            System.out.println(max);
        }
        System.gc();
    }
    private static int getLength(HashMap<Integer,Integer>[] maps,int u, int v,int max,int[] temp){

        Iterator<Map.Entry<Integer, Integer>> iterator = maps[u-1].entrySet().iterator();
        boolean flag = false;
        while(iterator.hasNext()){
            Map.Entry<Integer, Integer> next = iterator.next();
            if(next.getKey() == u) continue;
            if(temp[next.getKey()-1] ==1) continue;
            temp[next.getKey()-1] = 1;
            if(next.getKey() == v) {
                if(next.getValue() > max)
                    max = next.getValue();
                flag = true;
                break;
            }
            max = next.getValue();
            int length = getLength(maps, next.getKey(), v,max,temp);
            if(length > -1){
                flag = true;
                if(length > max)
                    max= length;
                break;
            }
        }
        if(flag)
        return max;
        else return -1;
    }
}
